package com.lyh.hkcloud.module.m1300090.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lyh.hkcloud.R;
import com.lyh.hkcloud.constants.BusinessConstants;

import java.util.List;
import java.util.Map;

/**
 * @program: hkCloud
 * @description:
 * @author: wwl
 * @create: 2024-05-03 10:28
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.VH> {

    private List<Map<String,Object>> dataMap;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<Map<String, Object>> dataMap) {
        this.dataMap=dataMap;
    }

    public RecyclerViewAdapter(List<Map<String,Object>> dataMap) {
        this.dataMap = dataMap;
    }
    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_listview_item_1300090, viewGroup, false);
        VH vh = new VH(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull VH vh, int i) {
        Map<String, Object> map = dataMap.get(i);
        Object cardNumber = map.get(BusinessConstants.CARD_NUMBER);
        vh.cardNumber.setText(cardNumber == null ? "" : cardNumber.toString());
        Object location = map.get(BusinessConstants.LOCATION_NAME);
        vh.location.setText(location == null ? "" : location.toString());
        Object usedPsn = map.get(BusinessConstants.USED_PSN);
        vh.usedPsn.setText(usedPsn == null ? "" : usedPsn.toString());
        Object computerName = map.get(BusinessConstants.COMPUTER_NAME);
        vh.computerName.setText(computerName == null ? "" : computerName.toString());
        Object physicalCode = map.get(BusinessConstants.PHYSICAL_CODE);
        vh.physicalCode.setText(physicalCode == null ? "" : physicalCode.toString());
        Object maNumber = map.get(BusinessConstants.MA_NUMBER);
        vh.maNumber.setText(maNumber == null ? "" : maNumber.toString());
        Object maName = map.get(BusinessConstants.MA_NAME);
        vh.maName.setText(maName == null ? "" : maName.toString());
        Object cardNum = map.get(BusinessConstants.CARD_NUM);
        vh.cardNum.setText(cardNum == null ? "" : cardNum.toString());
        Object inventoryNum = map.get(BusinessConstants.INVENTORY_NUM);
        vh.inventoryNum.setText(inventoryNum == null ? "" : inventoryNum.toString());
        Object memo = map.get(BusinessConstants.EXCEPTION_MEMO);
        vh.exceptionMemo.setText(memo == null ? "" : memo.toString());
    }

    @Override
    public int getItemCount() {
        return dataMap == null?0:dataMap.size();
    }

    public  class VH extends RecyclerView.ViewHolder{
        //实物码
       public TextView cardNumber;
        //位置
        public TextView location;
        //使用人信息
        public TextView usedPsn;
        //计算机名
        public TextView computerName;
        //资产编号
        TextView physicalCode;
        //物料编码
        TextView maNumber;
        //物料名称
        TextView maName;
        //资产数量
        TextView cardNum;
        //实盘数量
        EditText inventoryNum;
        //异常原因备注
        public TextView exceptionMemo;


        public VH(@NonNull View itemView) {
            super(itemView);
            cardNumber = itemView.findViewById(R.id.cardNumber);
            location = itemView.findViewById(R.id.location);
            usedPsn = itemView.findViewById(R.id.usedPsn);
            computerName = itemView.findViewById(R.id.computerName);
            physicalCode = itemView.findViewById(R.id.physicalCode);
            maNumber = itemView.findViewById(R.id.maNumber);
            maName = itemView.findViewById(R.id.maName);
            cardNum = itemView.findViewById(R.id.cardNum);
            inventoryNum = itemView.findViewById(R.id.inventoryNum);
            exceptionMemo = itemView.findViewById(R.id.exceptionMemo);

            //设置地址【修改按钮】点击事件
            setButtonEvent(VH.this,itemView,R.id.bt_update_location);
            //设置使用人【修改按钮】点击事件
            setButtonEvent(VH.this,itemView,R.id.bt_update_used);
            //设置异常原因【修改按钮】点击事件
            setButtonEvent(VH.this,itemView,R.id.bt_update_exceptionMemo);
        }
    }

    private void setButtonEvent(VH vh,View itemView,int id){
        View view =itemView.findViewById(id);
        //显示[修改]按钮组件
        view.setVisibility(View.VISIBLE);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener !=null) {
                    onItemClickListener.click(vh,view);
                }
            }
        });
    }
    public interface OnItemClickListener{

        void click(VH vh,View view);
    }
}
